package com.itboye.ihomebank.activity.key;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.TimesUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

/**
 * 修改锁有效时间
 */
public class UpdateKeyTimeActivity extends BaseOtherActivity implements Observer {
    ImageView close_icon;
    TextView add_shap_title_tv;
    LinearLayout kaishi_time, jieshu_time;
    TextView start_time, end_time, update_ok;

    private String lockId;//锁id
    private String keyId;//钥匙id
    private String start;//开始时间
    private String end;//结束时间

    private TimePickerView pvCustomTimeStart,pvCustomTimeEnd;
    long startT, endT;
    long currentTime;
    KeyPresenter presenter;
    String uid;

    public static String UPDATE_KEY = "update_key_ok";// 钥匙期限更改完成
    @Override
    protected int layout_id() {
        return R.layout.activity_update_key_time;
    }

    @Override
    protected void onInitController() {
    }

    @Override
    protected void onInit() {
        add_shap_title_tv.setText("更改有效期");
        presenter = new KeyPresenter(this);
        initTimePickerStart();
        initTimePickerEnd();
        lockId = getIntent().getStringExtra("lockid");
        keyId = getIntent().getStringExtra("keyid");
        start = getIntent().getStringExtra("start");
        end = getIntent().getStringExtra("end");
        startT=Long.parseLong(start);
        endT=Long.parseLong(end);
        currentTime = System.currentTimeMillis();
        uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
        start_time.setText(TimesUtils.timeStamp2Date(Long.parseLong(start)));
        end_time.setText(TimesUtils.timeStamp2Date(Long.parseLong(end)));
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.update_ok:
                if (String.valueOf(startT).equals(start)&&String.valueOf(endT).equals(end)){
                    ByAlert.alert("未做更改");
                    return;
                }
                if (endT <= startT) {
                    ByAlert.alert("结束时间应该大于开始时间");
                    return;
                }
                if (currentTime > endT) {
                    ByAlert.alert("结束时间应该大于当前时间");
                    return;
                }
                showProgressDialog("请稍后...", true);
                presenter.getYouXiaoQi(uid,keyId,lockId,startT/1000+"",endT/1000+"");

                break;
            case R.id.start_time:
            case R.id.kaishi_time:
                pvCustomTimeStart.show();
                break;
            case R.id.end_time:
            case R.id.jieshu_time:
                pvCustomTimeEnd.show();
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == KeyPresenter.qixian_success) {
                ByAlert.alert(entity.getData());
                Intent intent=new Intent(UPDATE_KEY);
                sendBroadcast(intent);
                finish();
            } else if (entity.getEventType() == KeyPresenter.qixian_fail) {
                ByAlert.alert(entity.getData());
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    private void initTimePickerStart() {
        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 具体可参考demo 里面的两个自定义layout布局。
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        Calendar startDate = Calendar.getInstance();
        startDate.set(2017, 1, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2027, 2, 28);
        //时间选择器 ，自定义布局
        pvCustomTimeStart = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调

                start_time.setText(getTime(date));
                startT =  TimesUtils.getStringToDateTwo02(getTime(date));
            }
        })
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)

                .setType(new boolean[]{true, true, true, true, false, false})
                .setLabel("年", "月", "日", "时", "", "")
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(0xFF24AD9D)
                .build();
    }
    private void initTimePickerEnd() {
        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 具体可参考demo 里面的两个自定义layout布局。
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        Calendar startDate = Calendar.getInstance();
        startDate.set(2017, 1, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2027, 2, 28);
        //时间选择器 ，自定义布局
        pvCustomTimeEnd = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调

                end_time.setText(getTime(date));
                endT =  TimesUtils.getStringToDateTwo02(getTime(date));
            }
        })
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)

                .setType(new boolean[]{true, true, true, true, false, false})
                .setLabel("年", "月", "日", "时", "", "")
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(0xFF24AD9D)
                .build();
    }
    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:00");
        return format.format(date);
    }
}
